<?php

class API_Controller extends Zend_Controller_Action{
    public static $tpl;
    private static $_params,
                   $_title,
                   $_title_delim,
                   $_title_add_right;
    
    public function init(){
        $this->_helper->viewRenderer->setNoRender(true);     
                                       
        if(!defined('SMARTY_DIR')){
            include_once 'Smarty/Smarty.class.php';
            $smarty = new Smarty();
            $smarty->debugging = false;
            $smarty->force_compile = true;
            $smarty->caching = false;
            $smarty->compile_check = true;
            $smarty->cache_lifetime = -1;
            $smarty->template_dir = '../tpl';
            $smarty->compile_dir = '../tpl_c';
            $smarty->plugins_dir = array(
              SMARTY_DIR . 'plugins',
              'resources/plugins');
            self::$tpl = $smarty;      
        }
                                                        
        self::$_title_delim = " » ";
        self::$_title_add_right = 1;
        self::setTitle("Биллинговая система");
        
        $path = self::getRequest()->getPathInfo(); 
        $path = explode("/", $path);
        self::assign('_controller', empty($path[1])?"index":$path[1]);
        self::assign('_action', empty($path[2])?"index":$path[2]);  
        self::$_params = array();      
        for($i=3; $i<count($path); $i++)
            self::$_params[] = $path[$i];
    }
    
    public function getPar($i){
        if(!isset(self::$_params[$i]))
            return "";
        return self::$_params[$i];
    }
    
    public function setTitle($title, $clear = false){
        $t = &self::$_title;
        if($clear)
            $t = '';
        if($t==''){
            $t = $title;
            return;
        }
        if(self::$_title_add_right)
            $t .= self::$_title_delim . $title;
        else
            $t = $title . self::$_title_delim . $t;
    }
    
    public function assign($key, $value){
        return self::$tpl->assign($key, $value);
    }
    
    public function parse($controller = '', $action = ''){
        $c = $controller;
        $a = $action;
        $params = self::_getAllParams();
        if($c!=''){
            if($a==''){
                $a = 'index';
            }
        } else {
            $c = $params['controller'];
            if($a==''){
                $a = $params['action'];
            }
        }
        $path = strtolower($c).'/'.strtolower($a).'.tpl';
        if($this->getResponse()->canSendHeaders())
            $this->getResponse()->setHeader('Content-Type', 'text/html; charset=utf-8');
        self::assign('page_title', self::$_title);
        self::$tpl->display($path);
    }
    
    public function go($url){
        header("Location: $url");
        exit;
    }
}